package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.embeddable.GoodsMatcherRuleRequestStruct
import com.zxy.supplier_system.server.service.MarketSettlementPlanService
import com.zxy.supplier_system.server.utils.IntIdAndName
import org.springframework.web.bind.annotation.*
import java.math.BigDecimal

@RestController
@RequestMapping("markets/{marketId}/settlementPlans")
class MarketSettlementPlanController(
    private val marketSettlementPlanService: MarketSettlementPlanService
) {

    data class ConfigureMarketSettlementPlanRequest(
        val rules: List<Rule> = emptyList(),
    ){
        data class Rule(
            val name: String,
            val note: String? = null,
            val commissionPercentage: BigDecimal? = null,
            val fixed: BigDecimal? = null,
            override val includedBrandIds: List<Int> = emptyList(),
            override val includedCategoryIds: List<Int> = emptyList(),
            override val includedBrandTagIds:List<Int> = emptyList(),
            override val includedGoodsIds: List<Int> = emptyList(),
        ): GoodsMatcherRuleRequestStruct
    }

    /**
     * 配置工资方案
     */
    @PostMapping
    fun configurePlan(@PathVariable marketId: Int, @RequestBody request:ConfigureMarketSettlementPlanRequest): ConfigureMarketSettlementPlanResponse{
        return  marketSettlementPlanService.configurePlan(marketId,request)
    }

    /**
     * 重新配置工资方案
     */
    @PutMapping
    fun reconfigurePlan(@PathVariable marketId: Int, @RequestBody request:ConfigureMarketSettlementPlanRequest): ConfigureMarketSettlementPlanResponse{
        return  marketSettlementPlanService.reconfigurePlan(marketId,request)
    }

    @GetMapping
    fun getPlan(@PathVariable marketId: Int): ConfigureMarketSettlementPlanResponse? {
        return marketSettlementPlanService.getPlanResponse(marketId)
    }

    data class ConfigureMarketSettlementPlanResponse(
        val rules: List<Rule> = emptyList(),
    ){
        data class Rule(
            val id: Int,
            val name: String,
            val note: String? = null,
            val commissionPercentage: BigDecimal? = null,
            val fixed: BigDecimal? = null,
            val includedBrands: List<IntIdAndName> = emptyList(),
            val includedCategories: List<IntIdAndName> = emptyList(),
            val includedBrandTags: List<IntIdAndName> = emptyList(),
            val includedGoodsList: List<GoodsItem> = emptyList(),
        ){
            data class GoodsItem(
                val id:Int,
                val name: String,
                val barcode: String,
            )
        }
    }
}